package cn.source.web.controller.majors;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.source.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.source.common.annotation.Log;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.enums.BusinessType;
import cn.source.majors.domain.SMajors;
import cn.source.majors.service.ISMajorsService;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 专业管理Controller
 *
 * @author xuhao
 * @date 2023-12-21
 */
@RestController
@RequestMapping("/majors/majors")
public class SMajorsController extends BaseController
{
    @Autowired
    private ISMajorsService sMajorsService;
    @Autowired
    private ISMajorsService userService;



    /**
     * 查询专业管理列表
     */
    @PreAuthorize("@ss.hasPermi('majors:majors:list')")
    @GetMapping("/list")
    public TableDataInfo list(SMajors sMajors)
    {
        startPage();
        List<SMajors> list = sMajorsService.selectSMajorsList(sMajors);
        return getDataTable(list);
    }

    /**
     * 导出专业管理列表
     */
    @PreAuthorize("@ss.hasPermi('majors:majors:export')")
    @Log(title = "专业管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SMajors sMajors)
    {
        List<SMajors> list = sMajorsService.selectSMajorsList(sMajors);
        ExcelUtil<SMajors> util = new ExcelUtil<SMajors>(SMajors.class);
        util.exportExcel(response, list, "专业管理数据");
    }

    /**
     * 导入功能
     */



    @PreAuthorize("@ss.hasPermi('majors:majors:importData')")
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SMajors> util = new ExcelUtil<SMajors>(SMajors.class);
        List<SMajors> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = getLoginUser();
        String operName = loginUser.getUsername();
        String message = userService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @PreAuthorize("@ss.hasPermi('majors:majors:importTemplate')")
    @Log(title = "专业管理", businessType = BusinessType.EXPORT)
    @RequestMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SMajors> util = new ExcelUtil<SMajors>(SMajors.class);
         util.importTemplateExcel(response, "物料数据");


    }



    /**
     * 获取专业管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('majors:majors:query')")
    @GetMapping(value = "/{mId}")
    public AjaxResult getInfo(@PathVariable("mId") Long mId)
    {
        return AjaxResult.success(sMajorsService.selectSMajorsByMId(mId));
    }

    /**
     * 新增专业管理
     */
    @PreAuthorize("@ss.hasPermi('majors:majors:add')")
    @Log(title = "专业管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SMajors sMajors)
    {
    	sMajors.setMcode(sMajors.getMcode().trim());
    	sMajors.setMname(sMajors.getMname().trim());
    	//判断专业编号是否存在，如果存在，提示重复
    	
    	SMajors sMajorsQuery=new SMajors();
    	sMajorsQuery.setMcode(sMajors.getMcode());
    	List<SMajors> list= sMajorsService.selectSMajorsList(sMajorsQuery);
    	if(list!=null&&list.size()>0) {
    		return AjaxResult.error("专业代码已存在，不能新增!");
    	}
    	 
    	return toAjax(sMajorsService.insertSMajors(sMajors));
    }

    /**
     * 修改专业管理
     */
    @PreAuthorize("@ss.hasPermi('majors:majors:edit')")
    @Log(title = "专业管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SMajors sMajors)
    {
    	sMajors.setMcode(sMajors.getMcode().trim());
    	sMajors.setMname(sMajors.getMname().trim());
        return toAjax(sMajorsService.updateSMajors(sMajors));
    }

    /**
     * 删除专业管理
     */
    @PreAuthorize("@ss.hasPermi('majors:majors:remove')")
    @Log(title = "专业管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{mIds}")
    public AjaxResult remove(@PathVariable Long[] mIds)
    {
        return toAjax(sMajorsService.deleteSMajorsByMIds(mIds));
    }
}
